import { KpiConfListVO, KpiConfListQuery } from '@/api/manu/kpiConfList/types';
import { listAllKpiConfList } from '@/api/manu/kpiConfList';
import {defineStore} from 'pinia';

export const useKpiStore = defineStore('kpi', ()=>{
  const state = reactive({
    kpiConfList: [] as KpiConfListVO[]
  });

  const refreshKpiConfgList = async () => {

    const res = await listAllKpiConfList();
    state.kpiConfList = [];
    state.kpiConfList=res.rows;
  }

  const getKpiConfList = (): KpiConfListVO[] => {
    const length = state.kpiConfList.length;
    if(length === 0) {
      refreshKpiConfgList();
    }
    return state.kpiConfList;
  }

  const getOptionsById = (id: number) => {
    const options = state.kpiConfList.find((item) => item.id === id).options.split(',');
    return options;
  }

  return {
    refreshKpiConfgList,
    getKpiConfList,
    getOptionsById
  }
})

export default useKpiStore;
